<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card shadow="hover" class="mgb20" style="height: 252px">
          <div class="user-info">
            <img src="../assets/img/img.jpg" class="user-avator" alt />
            <div class="user-info-cont">
              <div class="user-info-name">{{ name }}</div>
              <div>{{ role }}</div>
            </div>
          </div>
          <div class="user-info-list">
            上次登录时间：
            <span>2019-11-01</span>
          </div>
          <div class="user-info-list">
            上次登录地点：
            <span>东莞</span>
          </div>
        </el-card>
        <el-card shadow="hover" style="height: 252px">
          <template #header>
            <div class="clearfix">
              <span>语言详情</span>
            </div>
          </template>
          <div class="echart" id="mychart" :style="myChartStyle"></div>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-row :gutter="20" class="mgb20">
          <el-col :span="8">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-1">
                <i class="el-icon-user-solid grid-con-icon"></i>
                <div class="grid-cont-right">
                  <div class="grid-num">1234</div>
                  <div>用户访问量</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-2">
                <i class="el-icon-message-solid grid-con-icon"></i>
                <div class="grid-cont-right">
                  <div class="grid-num">321</div>
                  <div>系统消息</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="8">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-3">
                <i class="el-icon-s-goods grid-con-icon"></i>
                <div class="grid-cont-right">
                  <div class="grid-num">5000</div>
                  <div>数量</div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
        <el-card shadow="hover" style="height: 403px; width: 100%">
          <template #header>
            <div id="main"></div>
          </template>
        </el-card>
      </el-col>
    </el-row>

    <el-card shadow="hover" style="height: 300px; width: 30%">
      <el-table
        ref="multipleTableRef"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="标题名称" width="200">
          <template #default="scope">
            <a @click="tiao(scope.row.id)" style="color: blue">{{
              scope.row.titlename
            }}</a>
          </template>
        </el-table-column>
        <el-table-column property="issuetime" label="发布时间" width="250" />
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageindex"
        :page-sizes="[50, 100, 150, 200]"
        :page-size="pageize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import Schart from "vue-schart";
import { reactive, ref } from "vue";
import * as echarts from "echarts";
import { useRouter } from "vue-router";
import { onMounted } from "vue";
import { GetSiganalr } from "../../src/api/Inform.js";
export default {
  name: "dashboard",
  components: { Schart },
  setup() {
    const name = localStorage.getItem("ms_username");
    const role = name === "admin" ? "超级管理员" : "普通用户";
    const myChart = reactive([]);
    const pieData = ref([
      {
        value: 463,
        name: "江苏",
      },
      {
        value: 395,
        name: "浙江",
      },
      {
        value: 157,
        name: "山东",
      },
      {
        value: 149,
        name: "广东",
      },
      {
        value: 147,
        name: "湖南",
      },
    ]);
    const pieName = ref([]);
    const list = reactive({ value: [] });
    const myChartStyle = ref({ float: "left", width: "100%", height: "400px" }); //图表样式
    const tableData = ref([]);
    const formInline = reactive({
      pageindex: 1,
      pagesize: 10,
      TitleName: "",
      Issuer: "",
      IssueTime: "",
    });
    const count = ref(0);

    //跳转
    const router = useRouter();
    //声明周期函数，自动执行初始化
    onMounted(() => {
      init();
    });
    //初始化函数
    function init() {
      // 基于准备好的dom，初始化echarts实例
      let Chart = echarts.init(document.getElementById("main"));
      // 绘制图表
      let options = {
        title: {
          text: "本月任务图表",
        },
        tooltip: {},
        xAxis: {
          type: "category",
          data: list.value,
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: list.value,
          },
        ],
      };

      // 渲染图表
      Chart.setOption(options);
    }
    const initDate = () => {
      for (let i = 0; i < pieData.length; i++) {
        pieName[i] = pieData[i].name;
      }
    };
    initDate();
    const initEcharts = () => {
      // 饼图
      const option = {
        legend: {
          // 图例
          data: pieName,
          right: "10%",
          top: "30%",
          orient: "vertical",
        },
        title: {
          // 设置饼图标题，位置设为顶部居中
          text: "国内院士前五省份图示",
          top: "0%",
          left: "center",
        },
        series: [
          {
            type: "pie",
            label: {
              show: true,
              formatter: "{b} : {c} ({d}%)", // b代表名称，c代表对应值，d代表百分比
            },
            radius: "30%", //饼图半径
            data: pieData,
          },
        ],
      };
      myChart.values = echarts.init(document.getElementById("mychart"));
      myChart.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    };
    const show = () => {
      for (let i = 1; i <= 7; i++) {
        list.value.push(i);
      }
    };
    show();
    const signalRShow = () => {
      GetSiganalr(formInline).then((ros) => {
        console.log(ros);
        tableData.value = ros.items;
        count.value = ros.totalCount;
      });
    };
    signalRShow();
    const tiao = (id) => {
      console.log(id);
      router.push({ path: "/AnnouncementFind" ,query:{id:id}});
    };
    //分页点击事件
    const handleSizeChange = (val) => {
      formInline.pagesize = val;
    };
    //分页点击事件
    const handleCurrentChange = (val) => {
      formInline.pageindex = val;
    };
    const todoList = reactive([
      {
        title: "今天要修复100个bug",
        status: false,
      },
      {
        title: "今天要修复100个bug",
        status: false,
      },
      {
        title: "今天要写100行代码加几个bug吧",
        status: false,
      },
      {
        title: "今天要修复100个bug",
        status: false,
      },
      {
        title: "今天要修复100个bug",
        status: true,
      },
      {
        title: "今天要写100行代码加几个bug吧",
        status: true,
      },
    ]);

    return {
      name,
      todoList,
      role,
      pieData,
      formInline,
      pieName,
      myChartStyle,
      initDate,
      initEcharts,
      show,
      list,
      tableData,
      count,
      signalRShow,
      tiao,
      handleSizeChange,
      handleCurrentChange,
    };
  },
};
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
}

.grid-content {
  display: flex;
  align-items: center;
  height: 100px;
}
/* 样式这里要设置长宽，不然显示不出来 */
#main {
  width: 50vw;
  height: 30vh;
}
.grid-cont-right {
  flex: 1;
  text-align: center;
  font-size: 14px;
  color: #999;
}

.grid-num {
  font-size: 30px;
  font-weight: bold;
}

.grid-con-icon {
  font-size: 50px;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  color: #fff;
}

.grid-con-1 .grid-con-icon {
  background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
  color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
  background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
  color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
  background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
  color: rgb(242, 94, 67);
}

.user-info {
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  border-bottom: 2px solid #ccc;
  margin-bottom: 20px;
}

.user-avator {
  width: 120px;
  height: 120px;
  border-radius: 50%;
}

.user-info-cont {
  padding-left: 50px;
  flex: 1;
  font-size: 14px;
  color: #999;
}

.user-info-cont div:first-child {
  font-size: 30px;
  color: #222;
}

.user-info-list {
  font-size: 14px;
  color: #999;
  line-height: 25px;
}

.user-info-list span {
  margin-left: 70px;
}

.mgb20 {
  margin-bottom: 20px;
}

.todo-item {
  font-size: 14px;
}

.todo-item-del {
  text-decoration: line-through;
  color: #999;
}

.schart {
  width: 100%;
  height: 300px;
}
</style>
